<?php
/**
 * Created by PhpStorm.
 * User: CFH
 * Date: 2017/3/27
 * Time: 13:26
 */
class makeup_mdl extends Model{

    public static $table = 'mac_makeup';
    public static $tmpl_table = 'mac_makeup_tmpl';
    public static $item_table = 'mac_tm_items';


    public function add_json($sku_id,$title,$type,$color,$custom_color,$item_id,$tmpl_id,$tmpl_title,$tmpl_img){
        $sql = 'SELECT *
					FROM '.self::$table.'
					WHERE sku_id = '.$sku_id.' AND type ="'.$type.'" AND tmpl_id='.$tmpl_id;

        $item = $this->dbh->get_one($sql);
        if(empty($item['id'])){
                $this->dbh->insert(self::$table,
                    array(
                        'sku_id'=>$sku_id,
                        'title'=>$title,
                        'type'=>$type,
                        'color'=>$color,
                        'custom_color'=>$custom_color,
                        'item_id'=>$item_id,
                        'tmpl_id'=>$tmpl_id,
                        'tmpl_title'=>$tmpl_title,
                        'tmpl_img'=>$tmpl_img,
                        'create_time' => date('Y-m-d H:i:s')
                    )
                );
            $rs_id = $this->dbh->insert_id();
        }else {
            $this->dbh->update(self::$table,
                array(
                    'sku_id'=>$sku_id,
                    'title'=>$title,
                    'type'=>$type,
                    'color'=>$color,
                    'custom_color'=>$custom_color,
                    'item_id'=>$item_id,
                    'tmpl_id'=>$tmpl_id,
                    'tmpl_title'=>$tmpl_title,
                    'tmpl_img'=>$tmpl_img,
                    'create_time' => date('Y-m-d H:i:s')
                ),
                'id=' . $item['id']
            );
            $item = $this->get_one($item['id']);
        }
        return $item;
    }


    public function add_json_tmpl($tmpl_id,$type,$tmpl_title,$tmpl_img){

        $item = $this->get_one_tmpl($tmpl_id);
        if(empty($item['tmpl_id'])){
            $this->dbh->insert(self::$tmpl_table,
                array(
                    'tmpl_id'=>$tmpl_id,
                    'type'=>$type,
                    'tmpl_title'=>$tmpl_title,
                    'tmpl_img'=>$tmpl_img,
                    'create_time' => date('Y-m-d H:i:s')
                )
            );
            $rs_id = $this->dbh->insert_id();
        }else {
            $this->dbh->update(self::$tmpl_table,
                array(
                    'type'=>$type,
                    'tmpl_title'=>$tmpl_title,
                    'tmpl_img'=>$tmpl_img,
                    'create_time' => date('Y-m-d H:i:s')
                ),
                'tmpl_id=' . $tmpl_id
            );
            $item = $this->get_one_tmpl($tmpl_id);
        }
        return $item;
    }


    public function save_makeup($sku_id,$title,$type,$color,$custom_color,$item_id,$tmpl_id,$order,$hot){
        $sql = 'SELECT *
					FROM '.self::$table.'
					WHERE sku_id = '.$sku_id.' AND tmpl_id='.$tmpl_id;

        $item = $this->dbh->get_one($sql);
        if(empty($item['id'])){
            $this->dbh->insert(self::$table,
                array(
                    'sku_id'=>$sku_id,
                    'title'=>$title,
                    'type'=>$type,
                    'color'=>$color,
                    'custom_color'=>$custom_color,
                    'item_id'=>$item_id,
                    'tmpl_id'=>$tmpl_id,
                    'order_index'=>$order,
                    'hot'=>$hot,
                    'create_time' => date('Y-m-d H:i:s')
                )
            );
            $sql = 'SELECT *
					FROM '.self::$table.'
					WHERE sku_id = '.$sku_id.' AND tmpl_id='.$tmpl_id;

            $item = $this->dbh->get_one($sql);
        }else {
            $this->dbh->update(self::$table,
                array(
                    'sku_id'=>$sku_id,
                    'title'=>$title,
                    'type'=>$type,
                    'color'=>$color,
                    'custom_color'=>$custom_color,
                    'item_id'=>$item_id,
                    'tmpl_id'=>$tmpl_id,
                    'order_index'=>$order,
                    'hot'=>$hot,
                    'create_time' => date('Y-m-d H:i:s')
                ),
                'id=' . $item['id']
            );

            $sql = 'SELECT *
					FROM '.self::$table.'
					WHERE id = '.$item['id'];

            $item = $this->dbh->get_one($sql);
        }

        return $item;

    }


    //查所有试妆
    public function get_makeup($pageno, $pagesize, $cat, $keyword,$fields='*'){
        $sql_where='';
        $sql_where .= (!empty($cat))? " AND A.type = '".$cat."'" : "";
        $sql_where .= (!empty($keyword))? " AND B.title LIKE '%".$keyword ."%'": "";

        $sql = 'SELECT sku_id
				 FROM '.self::$table;
        $this->dbh->get_all($sql);
        $count = $this->dbh->rows_count;

        /*$sql = 'SELECT *
                FROM '.self::$table.'
                WHERE 1 = 1
				 '.$sql_where.' 
                LIMIT '.(($pageno-1)*$pagesize).' , '.$pagesize;*/

        $fields = 'A.id, B.item_imgs, B.title AS item_title, A.sku_id, A.title, A.type, A.color, A.custom_color, A.item_id, B.skus, A.tmpl_id, C.tmpl_title, C.custom_tmpl_title, C.tmpl_img, C.custom_tmpl_img, A.order_index, A.hot';

        $sql = 'SELECT '.$fields.' 
                  FROM '.self::$table.' A 
                  LEFT JOIN
                  '.self::$item_table.' B
                  ON A.item_id = B.num_iid
                  LEFT JOIN 
                  '.self::$tmpl_table.' C 
                  ON A.tmpl_id = C.tmpl_id
                  WHERE 1 = 1
				 '.$sql_where.'
				  ORDER BY CASE WHEN order_index<1 THEN 9999 end, order_index
				 LIMIT '.(($pageno-1)*$pagesize).' , '.$pagesize;

        $rs = $this->dbh->get_all($sql);

        return array(
            "total"=>$count,
            "list"=>$rs
        );
    }

    //查所有试妆ID
    public function get_makeups_id($type=''){
        $sql_where='';
        $sql_where .= (!empty($type))? " AND type = '".$type."'" : "";

        $sql = 'SELECT sku_id
				 FROM '.self::$table;

        $this->dbh->get_all($sql);
        $count = $this->dbh->rows_count;

        //$pageno = 1;
        $pagesize = 200;

        $ids = array();

        $pages = ceil($count / $pagesize);

        for($pageno = 1;$pageno <= $pages; $pageno++){
            $sql = 'SELECT id
                FROM '.self::$table.'
                WHERE 1 = 1
				 '.$sql_where.'
                LIMIT '.(($pageno-1)*$pagesize).' , '.$pagesize;

            $rs = $this->dbh->get_all($sql);
            array_push($ids, array_column($rs, 'id'));
        }

        return array(
            "total"=>$count,
            "list"=>$ids[0]
        );
    }


    public function get_one($sku_id){
        $r = '';
        if(!empty($sku_id)){
            $sql = 'SELECT *
					FROM '.self::$table.'
					WHERE sku_id = '.$sku_id;
            $r = $this->dbh->get_one($sql);
        }
        return $r;
    }

    public function get_one_tmpl($tmpl_id){
        $r = '';
        if(!empty($tmpl_id)){
            $sql = 'SELECT *
					FROM '.self::$tmpl_table.'
					WHERE tmpl_id = '.$tmpl_id;
            $r = $this->dbh->get_one($sql);
        }
        return $r;
    }

    public function get_tmpls($type){
        $sql_where='';
        $sql_where .= (!empty($type))? " AND type = '".$type."'" : "";

        $sql = 'SELECT tmpl_id
				 FROM '.self::$tmpl_table.'
                    WHERE 1 = 1
				 '.$sql_where;

        $this->dbh->get_all($sql);
        $count = $this->dbh->rows_count;

        //$pageno = 1;
        $pagesize = 200;

        $list = array();

        $pages = ceil($count / $pagesize);

        for($pageno = 1;$pageno <= $pages; $pageno++){
            $sql = 'SELECT *
                FROM '.self::$tmpl_table.'
                WHERE 1 = 1
				 '.$sql_where.'
                LIMIT '.(($pageno-1)*$pagesize).' , '.$pagesize;

            $rs = $this->dbh->get_all($sql);
            array_push($list, $rs);
        }

        return array(
            "total"=>$count,
            "list"=>$list[0]
        );
    }

    //修改试妆材质
    public function save_makeup_tmpl($tmpl_id, $custom_tmpl_title, $custom_tmpl_img){

        $this->dbh->update(self::$tmpl_table,
            array(
                'custom_tmpl_title'=>$custom_tmpl_title,
                'custom_tmpl_img'=>$custom_tmpl_img,
                'create_time' => date('Y-m-d H:i:s')
            ),
            'tmpl_id=' . $tmpl_id
        );
        $item = $this->get_one_tmpl($tmpl_id);
        return $item;
    }


    //删除不存在的试妆 SKU
    public function del_makeups_ids($del_ids)
    {
        $result = '';
        foreach ($del_ids as $id){
            $result = $this->dbh->delete(self::$table," sku_id = ".$id);
        }
        return $result;
    }

    //删除不存在的试妆 id
    public function del_makeups_ids_id($del_ids)
    {
        $result = '';
        foreach ($del_ids as $id){
            $result = $this->dbh->delete(self::$table," id = ".$id);
        }
        return $result;
    }


}